import urllib2
import json

URL='https://btc-e.com/api/2/ltc_usd/ticker'

def getticker():
    try:
        req = urllib2.Request(URL)
        con = urllib2.urlopen(req)
        if con.getcode() != 200: raise RuntimeError('retcode is not OK')
        buf = con.readlines()
        ret = json.loads(buf[0])['ticker']
        print 'getticker: current LTC/USD rate is ' + str(ret['last'])
        return ret
    except Exception, err:
        print 'getticker failed: ' + str(err)
        return None

if __name__ == '__main__':
    import pdb
    print getticker()
